<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Com_search extends CI_Controller {
	private $per_page = 5;
	private $total_rows = '';
	private $uri_page = '';
	private $base_page = '';
	private $uri_segment = '';
	function __construct() {
		parent::__construct();
	}
	function index() {
		$offset = $this->uri->segment(2);
		if($this->input->post('search')){
			$this->session->set_userdata('search', $this->input->post('search'));
		}
		if($this->input->post('search')){
			$this->session->set_userdata('province', $this->input->post('province'));
		}
		$search = $this->session->userdata('search');
		$province = $this->session->userdata('province');
		$subjects_id = '';
		if(!empty($search)){
			$subjects_id = ','.$this->common_model->getone('subjects', array('title'=>$search),'',array('title LIKE'=>'%'.$search.'%'))->id.',';
		}
		$ketqua_array = array(
				'type' => 1,
				'activated' => 1,
				'publish' => 1,
				'status' => 1);
		$ketqua_array['subjects LIKE'] = '%'.$subjects_id.'%';
		if(!empty($province)){
			$ketqua_array['city'] = $province;
		}
		$ketqua = $this->common_model->getdata('users', $ketqua_array, '', '', array(
				$this->per_page,
				$offset
			));
		$this->total_rows = $this->common_model->getnumber2('users', $ketqua_array);
		if (!empty($key)) {
			$this->db->like('title', $key);
			$monhoc = $this->common_model->getone('subjects');
			$where2 = '';
			if (!empty($monhoc)) {
				$where = 'subjects LIKE "%' . $monhoc->id . '%"';
				$this->db->where($where, NULL, FALSE);
				$this->db->from('users');
			}
		}
		$this->phantrang();
		$data['content'] = $this->load->view('com_search/view_search', array(
			'tutors' => $ketqua,
			'numturtor' => $this->total_rows,
			'search' => $search,
			'province' => $province,
			'subjects_id' => $subjects_id
		), true);
		$this->load->view('index', $data);
	}
	
	function timkiem() {
		$sub1   = $_POST['sub1'];
		$sub2   = $_POST['sub2'];
		$sub3   = $_POST['sub3'];
		$tien1  = $_POST['tien1'];
		$tien2  = $_POST['tien2'];
		$tuoi1  = $_POST['tuoi1'];
		$tuoi2  = $_POST['tuoi2'];
		$city   = $_POST['city'];
		$dist   = $_POST['dist'];
		$gender = $_POST['gender'];
		$subid1 = $this->common_model->getid('subjects', array(
			'title LIKE' => $sub1
		));
		$subid2 = $this->common_model->getid('subjects', array(
			'title LIKE' => $sub2
		));
		$subid3 = $this->common_model->getid('subjects', array(
			'title LIKE' => $sub3
		));
		$year   = date("Y");
		if ($gender != 3) {
			$this->db->where('gender', $gender);
		}
		$aray = array(
			'mucluong >=' => $tien1,
			'mucluong <=' => $tien2,
			'year <=' => $year - $tuoi1 + 1,
			'year >=' => $year - $tuoi2 - 1,
		);
		$this->db->where($aray);
		if (!empty($city)) {
			if ($dist != 0) {
				$this->db->where('district', $dist);
			} elseif ($city != 0) {
				$this->db->where('city', $city);
			}
		}
		if (!empty($subid1) || !empty($subid2) || !empty($subid3)) {
			$where1 = 'subjects REGEXP ",' . $subid1 . ',|,' . $subid2 . ',|,' . $subid3 . ',"';
			$this->db->where($where1, NULL, false);
		}
		$ketqua = $this->common_model->get_data('users', array(
			'type' => 1,
			'activated' => 1,
			'publish' => 1,
			'status' => 1
		));
		return $this->load->view('com_search/ketqua.php', array(
			'ketqua' => $ketqua,
			'numturtor' => count($ketqua),
			'subjects_id' => $subid1
		));
	}
	function phantrang() {
		$this->load->library('pagination');
		$config['base_url']    = base_url() . $this->uri->segment(1);
		$config['total_rows']  = $this->total_rows;
		$config['per_page']    = $this->per_page;
		$config['uri_segment'] = 2;
		$config['num_links']   = 12;
		$this->pagination->initialize($config);
	}
}